package frame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class IntroductionPanel extends JPanel {
    private MainFrame frame;
    private BufferedImage introductionImage;

    public IntroductionPanel(MainFrame frame) {
        this.frame = frame;
        this.setLayout(new BorderLayout());

        // 创建一个新的 JPanel, 其布局方式设为 BorderLayout
        JPanel fillerNorth = new JPanel(new BorderLayout());
        fillerNorth.setOpaque(false);
        this.add(fillerNorth,BorderLayout.CENTER);

        // 获取图片图片
        try {
            introductionImage = ImageIO.read(new File ("src/static/游戏规则.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 创建返回按钮
        JButton backButton = new JButton("返回");
        backButton.setPreferredSize(new Dimension(40, 50));
        backButton.setBackground(new Color(255, 255, 255)); // 设置按钮背景色
        backButton.setFont(new Font("Courier", Font.BOLD, 28));  // 设置按钮字体样式
        backButton.setForeground(new Color(72, 72, 72));  // 设置按钮字体颜色
        backButton.setOpaque(true);  // 设置按钮为不透明
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 返回初始菜单
                frame.setContentPane(new MainViewPanel(frame));
                frame.invalidate();
                frame.validate();
            }
        });

        // 在中线位置添加返回按钮
        fillerNorth.add(backButton,BorderLayout.SOUTH);
    }

    // 重写 paintComponent 方法以便在返回按钮之上绘制背景图。
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(introductionImage, 0, 0, 800, 560, null);
    }
}
